package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.adapters.JobFairListAdapter;
import com.njrsrc.android.bean.JobFairInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.ptr.PullToRefreshBase;
import com.njrsrc.android.library.ptr.PullToRefreshBase.Mode;
import com.njrsrc.android.library.ptr.PullToRefreshBase.OnRefreshListener;
import com.njrsrc.android.library.ptr.PullToRefreshListView;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.JobFairTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 招聘会
 * 
 * @author Administrator
 * 
 */
public class JobFairActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private UserInfo userInfo;
	private ProgressDialog progressDialog;
	private PullToRefreshListView jobListView;
	private JobFairListAdapter adapter;
	private int PAGE_ID = 1;
	/** 每页显示的条数 */
	private static final int COUNT = 10;
	private boolean loadingEnd = false;
	private ArrayList<JobFairInfo> jobFairList;
	private String requestUrl;
	private boolean initLoad = true;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.JOB_FAIR_QUERY_ERROR:
				Helpers.closeProgressDialog(progressDialog);
				Toast.makeText(JobFairActivity.this, 
						getResources().getString(R.string.connectError), 
						Toast.LENGTH_SHORT).show();
				break;
			case MsgID.JOB_FAIR_QUERY_FAIL:
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.JOB_FAIR_QUERY_SUCCESS:
				Helpers.closeProgressDialog(progressDialog);
				jobListView.onRefreshComplete();
				adapter.setData(jobFairList);
				adapter.notifyDataSetChanged();
				
				if(jobFairList != null && jobFairList.size() != 0 && initLoad)
				{
					int pos = -1;
					for(int i = 0;i < jobFairList.size();i++)
					{
						JobFairInfo jobFairInfo = jobFairList.get(i);
						if(Helpers.isFutureTime(jobFairInfo.getStart_at()))
						{
							pos = i;
							break;
						}
					}
					
					if(pos != -1)
					{
						//组建内部有headerview的起始位置是0
						jobListView.getRefreshableView().setSelectionFromTop(pos + 1, 0);
					}
					initLoad = false;
				}
				clear();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_fair);
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("招聘会");
		mBar.back.setOnClickListener(this);
		jobListView = (PullToRefreshListView) findViewById(R.id.job_listView);
		jobListView.setScrollingWhileRefreshingEnabled(!jobListView.isScrollingWhileRefreshingEnabled());
		jobListView.setMode(Mode.PULL_FROM_END);
		jobListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
 			@Override
 			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
 				if(!loadingEnd)
 				{
 					initData();
 				}
 			}
 		});
		
		adapter = new JobFairListAdapter(JobFairActivity.this);
		jobListView.setAdapter(adapter);
		
		progressDialog = Helpers.showProgressDialog(JobFairActivity.this, R.string.loading);
		initData();
		
		jobListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String jobFairId = (String) arg1.getTag(R.id.job_fair_id);
				
				Intent intent = new Intent();
				intent.setClass(JobFairActivity.this, JobBoothActivity.class);
				Bundle data = new Bundle();
				data.putString("jobFairId", jobFairId);
				intent.putExtras(data);
				startActivity(intent);
				
			}
			
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void initData()
	{
		List<String> params = new ArrayList<String>();
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		params.add("0");
		params.add(PAGE_ID + "");
		params.add(COUNT + "");
		requestUrl = RequestUrlProvider.JOB_FAIR_LIST;
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		jobFairList = new ArrayList<JobFairInfo>();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = jobFairList;
		new JobFairTask().execute(args);
	}
	
	private void clear()
	{
		if(jobFairList == null || jobFairList.size() == 0 || jobFairList.size() < COUNT)
        {
        	loadingEnd = true;
        	jobListView.setMode(Mode.DISABLED);
        }else
        {
        	PAGE_ID++;
        }
		jobFairList.clear();
		jobFairList = null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		default:
			break;
		}
	}
}